<?php
/**
 * 素材管理
 * @author 七里枫香
 *
 */
namespace Member\Controller;

//数据分页
use Think\Page;

class MaterialController extends MemberController {

	/**
	 * 获取图文消息列表信息
	 */
	public function index(){
		
		$map = array(
				'uid'=>UID,
				'relation'=>''
		);
		 
		$newsmsgLogic = D('Newsmsg','Logic');
		 
		$count = $newsmsgLogic->where($map)->count();
		$page = new Page($count,20);
		$show       = $page->show();// 分页显示输出
		// 		dump($show);exit();
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $newsmsgLogic->where($map)->order('id ASC')->limit($page->firstRow.','.$page->listRows)->select();
		
		foreach ($list as $k=>&$v){
		    $v['configs'] = serializeMysql($v['configs'],1);
		}
		
// 		dump($list);exit;
		
		$this->assign('lists',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		
		$this->display();
		
	}

	/**
	 * 单条图文
	 */
	public function onenews($id = 0){
		$id = intval($id);
		if($id > 0){
			$newsmsg = D('Newsmsg','Logic');
			$onenews = $newsmsg->detail($id);
			
			$onenews['configs'] = serializeMysql($onenews['configs'],1);
			
			$this->assign('onenews',$onenews);
			
// 			dump($onenews);
			
			$this->assign('update',1);
			
		}else{
		    $this->assign('update',0);
		}
		
		//获取链接地址
		$this->linkurl();
		
		$this->display();
		 
	}
	
	public function newsSave(){
	    $newsmsg = D('Newsmsg','Logic');
	    
// 	    dump($_POST);exit();
	    
	    //类型
	    $msgtype = I('post.msgtype');
	    
	    //如果是单图文信息
	    if($msgtype == 'onenews'){
	    	$boolean = $newsmsg->saveOnenews();
	    }else if($msgtype == 'news'){
	    	//保存多图文消息
	        $boolean = $newsmsg->saveNews();
	    }
	    
	    if($boolean){
	        $this->success('成功');
	    }else{
	        $this->error('失败');
	    }
	}
	
	
	/**
	 * 多条图文
	 */
	public function news($id = 0){
		
		$id = intval($id);
		
		if($id > 0){
			$newsmsg = D('Newsmsg','Logic');
			$news = $newsmsg->detail($id);
			
			$news['configs'] = serializeMysql($news['configs'],1);
			
			//获取第一个图文
			$first = array_shift($news['configs']);
			$others = $news['configs'];
			
			$this->assign('id',$news['id']);
			$this->assign('createtime',$news['createtime']);
			$this->assign('firstNews',$first);
			$this->assign('othersNews',$others);
				
			$this->assign('update',1);
				
		}else{
			$this->assign('update',0);
		}
		
		//获取链接地址
		$this->linkurl();
		
		
		$this->display();
			
	}
	
	/**
	 * 删除一条图文消息
	 * @param number $id
	 */
	public function delNewmsg($id = 0){
		$id = intval($id);
		$boolean = false;
		if($id > 0){
			$newsmsg = D('Newsmsg','Logic');
			$boolean = $newsmsg->del($id);
		}
		
		if($boolean){
		    $this->success('删除成功');
		}else{
		    $this->error('删除失败');
		}
	}
	
	
	/**
	 * url管理
	 */
	public function url($id = 0){
		
		
		$url = D('Urls','Logic');
		
		$id = intval($id);
		if($id > 0){
		    $theurl = $url->detail($id);
		    $this->assign('theurl',$theurl);
		    $this->assign('update',1);
		}
		
		///////分页显示数据   开始/////////////////
		//查询条件
		$map = array(
		    'uid'=>UID,
		);
		
		$count = $url->where($map)->count();
		$page = new Page($count,20);
		$show       = $page->show();// 分页显示输出
// 		dump($show);exit();
    	// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
    	$list = $url->where($map)->order('id ASC')->limit($page->firstRow.','.$page->listRows)->select();
    	$this->assign('lists',$list);// 赋值数据集
    	$this->assign('page',$show);// 赋值分页输出
		
		///////分页显示数据   结束/////////////////
		
		
	    $this->display();
	}
	
	/**
	 * 修改或者添加Url
	 */
	public function urlsSave(){
		$url = D('Urls','Logic');
		$boolean = $url->update();
		
		if($boolean){
		    redirect(U('url'));
		}else{
		    $this->error('添加错误');
		}
	}
	
	/**
	 * 删除url
	 */
	public function urlsDel($id){
		$url = D('Urls','Logic');
		$boolean = $url->del($id);
		
		if($boolean){
		    $this->success('删除成功');
		}else{
		    $this->error('删除失败');
		}
	}
	
	
	
    /**
     * 系统URL查询
     */
	public function sysurl(){
		
		$type = I('get.type');
		if(empty($type)){
			//获取全部的模块信息
			$moudleurlLogic = D('Module','Logic');
			$modules = $moudleurlLogic->allLists();
// 					dump($modules);

			$useropenmodules = session('usermodules');
			foreach ($modules as $k=>&$v){
				$v['moudle'] = $v['name'];
				if(!in_array(strtolower($v['name']),$useropenmodules)){
					unset($modules[$k]);
				}
			}
			$this->assign('urls',$modules);
		}else if($type == 'home'){
			//本地生活
			$url[0]['title'] = '本地生活';
			$homeurl['title'] = '微主页';
			$homeurl['url'] = 'home/index';
			$couponurl1['url'] = 'coupon/index/pid/1';
			$couponurl1['title'] = '餐饮美食';
			$couponurl2['url'] = 'coupon/index/pid/2';
			$couponurl2['title'] = '休闲娱乐';
			$couponurl3['url'] = 'coupon/index/pid/3';
			$couponurl3['title'] = '酒店宾馆';
			$couponurl4['url'] = 'coupon/index/pid/4';
			$couponurl4['title'] = "生活服务";
			$activity['title'] = "本地活动行";
			$activity['url'] = 'activity/index';
			$carpooling['title'] = "本地拼车";
			$carpooling['url'] = 'carpooling/hascar';
			$url[0]['menupages']= array(
					'0'	=> $homeurl,
					'1'	=> $couponurl1,
					'2' => $couponurl2,
					'3' => $couponurl3,
					'4' => $couponurl4,
					'5' => $activity,
					'6'	=> $carpooling,
			);
			$this->assign('urls',$url);
		}else if($type == 'business'){
			//微商家
			$url[1]['title'] = '微商家';
			$businessurl['title'] = '微商家';
			$businessurl['url'] = 'business/index';
			$codeurl['url'] = 'code/codelist';
			$codeurl['title'] = '商家"扫"知道';
			$url[1]['menupages']= array(
					'0'	=> $businessurl,
					'1'	=> $codeurl,
			);
			$this->assign('urls',$url);
		}else if($type == 'info'){
			
			$url[2]['title'] = '本地信息';
			
			$recruiturl['title'] = '求职招聘';
			$recruiturl['url'] = 'recruit/index';
			
			$secondurl['url'] = 'second/index';
			$secondurl['title'] = '二手物品';
			
			$house['url'] = 'house/index';
			$house['title'] = '房屋租赁';
			
			$euction['url'] = 'education/index';
			$euction['title'] = '教育培训';
			
			$property['url'] = 'property/index';
			$property['title'] = '本地房产';
			
			$yellow['url'] = 'yellowpages/index';
			$yellow['title'] = '本地黄页';
			
			$wedding['url'] = 'wedding/index';
			$wedding['title'] = '本地婚庆';
			
			$medical['url'] = 'medical/index';
			$medical['title'] = '微医疗';
			
			$tel['url'] = 'tel/index';
			$tel['title'] = '本地电话';
			
			$url[2]['menupages']= array(
					'0'	=> $recruiturl,
					'1'	=> $secondurl,
					'2' => $house,
					'3'	=> $euction,
					'4'	=> $property,
					'5'	=> $yellow,
					'6'	=> $wedding,
					'7'	=> $medical,
					'8'	=> $tel,
			);
			$this->assign('urls',$url);
		}else if($type == 'news'){
			//城市新闻
			$url[3]['title'] = '城市新闻';
			$url[3]['moudle'] = 'news/index';
			$this->assign('urls',$url);
		}else if($type == 'history'){
			$url[4]['title'] = '城市历史';
			$url[4]['moudle'] =  'history/index';
			$this->assign('urls',$url);
		}else if($type=='baby'){
			$url[5]['title'] = '萌宝大赛';
			$url[5]['moudle'] =  'baby/index/type/baby';
			$url[6]['title'] = '最美笑脸';
			$url[6]['moudle'] =  'baby/index/type/face';
			$this->assign('urls',$url);
		}else if($type=='quotation'){
			$url[5]['title'] = '微语录';
			$url[5]['moudle'] =  'quotation/index';
			$this->assign('urls',$url);
		}else if($type=='dating'){
			$url[5]['title'] = '本地交友';
			$url[5]['moudle'] =  'dating/myindex';
			$this->assign('urls',$url);
		}else if($type=='nlmy'){
			$url[5]['title'] = '农林牧渔';
			$url[5]['moudle'] =  'nlmy/index';
			$this->assign('urls',$url);
		}
		
		
		$this->assign('type',$type);
	    $this->display();
	}
	

}
